<?php

namespace App\Http\Controllers\Api;

use App\Models\Category;
use Illuminate\Http\Request;

class CategoriesController extends BaseController
{
    public function index(Request $request)
    {
        $categories = Category::query()->where('deleted',0)->where('parent_id',0)->orderBy('order','asc')->get();

        $data = [];
        if(!empty($categories))
        {
            foreach ($categories as $v)
            {
                $data[] = [
                    'id' => $v['id'],
                    'title' => $v['title'],
                    'book_total' => $v['book_total'],
                    'logo' => config('app.qiniu_image_domain').'/'.$v['logo']
                ];
            }
        }

        return $this->res([
            'data' => $data
        ]);
    }

    public function secondCategories(Request $request)
    {
        $parent_id = intval($request->input('parent_id'));
        $categories = Category::query()->where('deleted',0)->where('parent_id',$parent_id)->orderBy('order','asc')->get();

        $data = [];
        if(!empty($categories))
        {
            foreach ($categories as $v)
            {
                $data[] = [
                    'id' => $v['id'],
                    'title' => $v['title'],
                ];
            }
        }

        return $this->res([
            'data' => $data
        ]);
    }
}
